Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.3 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.3, n = 539)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 12.94215 12.94289 12.94364 12.94439 12.94514 12.94588 12.94662 12.94734
## [9] 12.94804 12.94872 12.94937 12.94998 12.95056 12.95110 12.95158 12.95202
## [17] 12.95240 12.95272 12.95298 12.95316 12.95327 12.95330 12.95325 12.95311
## [25] 12.95287 12.95254 12.95210 12.95156 12.95091 12.95014 12.94925 12.94827
## [33] 12.94718 12.94601 12.94475 12.94341 12.94200 12.94054 12.93901 12.93744
## [41] 12.93583 12.93418 12.93251 12.93081 12.92910 12.92739 12.92568 12.92397
## [49] 12.92229 12.92062 12.91898 12.91738 12.91583 12.91432 12.91287 12.91149
## [57] 12.91018 12.90883 12.90733 12.90570 12.90394 12.90207 12.90010 12.89804
## [65] 12.89590 12.89370 12.89143 12.88913 12.88679 12.88444 12.88207 12.87970
## [73] 12.87735 12.87503 12.87274 12.87050 12.86831 12.86620 12.86417 12.86224
## [81] 12.86041 12.85870 12.85712 12.85567 12.85438 12.85265 12.84995 12.84634
## [89] 12.84191 12.83673 12.83089 12.82446 12.81751 12.81013 12.80239 12.79438
## [97] 12.78616 12.77781 12.76942 12.76106 12.75280 12.74474 12.73693 12.72947
## [105] 12.72242 12.71587 12.70989 12.70457 12.69997 12.69618 12.69327 12.69132
## [113] 12.69042 12.69000 12.68950 12.68895 12.68838 12.68783 12.68734 12.68692
## [121] 12.68663 12.68649 12.68654 12.68682 12.68735 12.68817 12.68931 12.69081
## [129] 12.69271 12.69504 12.69782 12.70110 12.70491 12.70928 12.71426 12.72103
## [137] 12.73051 12.74231 12.75600 12.77119 12.78748 12.80445 12.82170 12.83882
## [145] 12.85542 12.87109 12.88542 12.89800 12.90843 12.91886 12.93146 12.94586
## [153] 12.96170 12.97862 12.99625 13.01424 13.03222 13.04982 13.06669 13.08245
## [161] 13.09675 13.10923 13.11951 13.13024 13.14400 13.16043 13.17912 13.19970
## [169] 13.22176 13.24494 13.26883 13.29305 13.31721 13.34092 13.36380 13.38546
## [177] 13.40551 13.42356 13.43923 13.45212 13.46185 13.46803 13.47222 13.47621
## [185] 13.47994 13.48337 13.48645 13.48913 13.49136 13.49309 13.49428 13.49488
## [193] 13.49483 13.49409 13.49261 13.49034 13.48723 13.48324 13.47831 13.47240
## [201] 13.46545 13.45743 13.44827 13.43794 13.42638 13.41263 13.39598 13.37682
## [209] 13.35551 13.33243 13.30794 13.28241 13.25621 13.22972 13.20330 13.17732
## [217] 13.15216 13.12818 13.10575 13.08135 13.05177 13.01781 12.98028 12.94001
## [225] 12.89779 12.85446 12.81080 12.76765 12.72581 12.68610 12.64933 12.61630
## [233] 12.58784 12.55978 12.52786 12.49281 12.45532 12.41610 12.37587 12.33533
## [241] 12.29519 12.25616 12.21895 12.18425 12.15279 12.12528 12.10241 12.08261
## [249] 12.06383 12.04600 12.02908 12.01301 11.99776 11.98327 11.96949 11.95638
## [257] 11.94388 11.93194 11.92053 11.90958 11.89906 11.89085 11.88651 11.88545
## [265] 11.88709 11.89083 11.89610 11.90231 11.90887 11.91521 11.92072 11.92483
## [273] 11.92696 11.92651 11.92291 11.91905 11.91790 11.91901 11.92192 11.92617
## [281] 11.93128 11.93681 11.94229 11.94726 11.95125 11.95381 11.95448 11.95278
## [289] 11.94827 11.94289 11.93876 11.93565 11.93333 11.93156 11.93013 11.92880
## [297] 11.92734 11.92552 11.92312 11.91990 11.91563 11.91009 11.90305 11.89410
## [305] 11.88314 11.87040 11.85607 11.84038 11.82353 11.80574 11.78723 11.76819
## [313] 11.74886 11.72943 11.71013 11.69117 11.67275 11.65509 11.63841 11.62292
## [321] 11.60883 11.59635 11.58229 11.56383 11.54169 11.51663 11.48937 11.46067
## [329] 11.43126 11.40188 11.37327 11.34617 11.32133 11.29948 11.28136 11.26772
## [337] 11.25929 11.25313 11.24604 11.23834 11.23036 11.22242 11.21486 11.20801
## [345] 11.20218 11.19772 11.19494 11.19418 11.19577 11.20003 11.20724 11.21726
## [353] 11.22974 11.24438 11.26085 11.27883 11.29800 11.31802 11.33859 11.35938
## [361] 11.38006 11.40031 11.41982 11.43826 11.45801 11.48140 11.50796 11.53726
## [369] 11.56883 11.60224 11.63703 11.67275 11.70895 11.74519 11.78100 11.81595
## [377] 11.84959 11.88145 11.91111 11.93809 11.96615 11.99877 12.03521 12.07466
## [385] 12.11637 12.15954 12.20341 12.24720 12.29013 12.33143 12.37032 12.40601
## [393] 12.43775 12.46474 12.49115 12.52118 12.55413 12.58930 12.62597 12.66344
## [401] 12.70101 12.73797 12.77362 12.80725 12.83815 12.86563 12.88897 12.90748
## [409] 12.92291 12.93747 12.95116 12.96396 12.97586 12.98686 12.99693 13.00607
## [417] 13.01427 13.02150 13.02777 13.03305 13.03734 13.04063 13.04156 13.03912
## [425] 13.03372 13.02583 13.01588 13.00431 12.99157 12.97810 12.96433 12.95072
## [433] 12.93770 12.92571 12.91521 12.90662 12.89688 12.88301 12.86566 12.84544
## [441] 12.82299 12.79893 12.77389 12.74850 12.72338 12.69917 12.67649 12.65596
## [449] 12.63822 12.62389 12.60955 12.59174 12.57104 12.54806 12.52339 12.49762
## [457] 12.47133 12.44513 12.41960 12.39534 12.37294 12.35299 12.33608 12.32281
## [465] 12.31164 12.30068 12.28994 12.27944 12.26922 12.25928 12.24967 12.24040
## [473] 12.23149 12.22297 12.21486 12.20718 12.19996 12.19322 12.18710 12.18167
## [481] 12.17688 12.17266 12.16897 12.16574 12.16293 12.16047 12.15830 12.15638
## [489] 12.15464 12.15303 12.15149 12.14997 12.14867 12.14782 12.14740 12.14740
## [497] 12.14780 12.14858 12.14974 12.15124 12.15308 12.15524 12.15771 12.16046
## [505] 12.16348 12.16676 12.17035 12.17434 12.17870 12.18343 12.18854 12.19400
## [513] 12.19982 12.20599 12.21250 12.21935 12.22653 12.23403 12.24185 12.24998
## [521] 12.25841 12.26715 12.27618 12.28549 12.29509 12.30491 12.31495 12.32520
## [529] 12.33568 12.34641 12.35739 12.36865 12.38019 12.39203 12.40419 12.41667
## [537] 12.42949 12.44267 12.45621
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.3, n = 539)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.57826 12.57605 12.57391 12.57185 12.56986 12.56794 12.56608 12.56428
## [9] 12.56254 12.56086 12.55923 12.55765 12.55611 12.55462 12.55317 12.55176
## [17] 12.55038 12.54904 12.54772 12.54643 12.54516 12.54392 12.54269 12.54147
## [25] 12.54027 12.53907 12.53788 12.53669 12.53550 12.53430 12.53305 12.53179
## [33] 12.53050 12.52920 12.52789 12.52658 12.52527 12.52397 12.52269 12.52143
## [41] 12.52020 12.51901 12.51786 12.51676 12.51571 12.51472 12.51379 12.51294
## [49] 12.51217 12.51148 12.51088 12.51038 12.50998 12.50969 12.50952 12.50947
## [57] 12.50955 12.50966 12.50973 12.50976 12.50977 12.50975 12.50973 12.50970
## [65] 12.50969 12.50970 12.50974 12.50982 12.50994 12.51013 12.51038 12.51072
## [73] 12.51114 12.51166 12.51228 12.51303 12.51390 12.51490 12.51605 12.51736
## [81] 12.51884 12.52048 12.52232 12.52434 12.52657 12.52874 12.53058 12.53212
## [89] 12.53340 12.53444 12.53528 12.53593 12.53643 12.53682 12.53711 12.53733
## [97] 12.53752 12.53771 12.53792 12.53819 12.53853 12.53899 12.53959 12.54035
## [105] 12.54131 12.54250 12.54395 12.54568 12.54772 12.55011 12.55287 12.55603
## [113] 12.55962 12.56311 12.56601 12.56840 12.57037 12.57199 12.57335 12.57454
## [121] 12.57563 12.57671 12.57786 12.57916 12.58070 12.58256 12.58482 12.58757
## [129] 12.59088 12.59484 12.59953 12.60504 12.61144 12.61998 12.63154 12.64566
## [137] 12.66188 12.67974 12.69879 12.71857 12.73862 12.75848 12.77769 12.79581
## [145] 12.81236 12.82689 12.83894 12.85188 12.86891 12.88938 12.91262 12.93798
## [153] 12.96477 12.99235 13.02004 13.04719 13.07313 13.09720 13.11873 13.13707
## [161] 13.15154 13.16557 13.18266 13.20224 13.22371 13.24653 13.27009 13.29385
## [169] 13.31721 13.33961 13.36047 13.37922 13.39528 13.40808 13.41705 13.42367
## [177] 13.42982 13.43548 13.44061 13.44519 13.44921 13.45264 13.45545 13.45763
## [185] 13.45915 13.45999 13.46013 13.45953 13.45819 13.45607 13.45316 13.44943
## [193] 13.44485 13.43941 13.43308 13.42585 13.41767 13.40855 13.39844 13.38733
## [201] 13.37519 13.36201 13.34776 13.33241 13.31435 13.29239 13.26716 13.23926
## [209] 13.20933 13.17797 13.14581 13.11347 13.08157 13.05072 13.02155 12.99468
## [217] 12.97072 12.94544 12.91480 12.87966 12.84092 12.79944 12.75612 12.71183
## [225] 12.66746 12.62388 12.58197 12.54262 12.50671 12.47512 12.44873 12.42358
## [233] 12.39553 12.36514 12.33300 12.29968 12.26576 12.23180 12.19839 12.16610
## [241] 12.13551 12.10719 12.08171 12.05966 12.04161 12.02671 12.01361 12.00213
## [249] 11.99207 11.98327 11.97553 11.96868 11.96253 11.95689 11.95159 11.94643
## [257] 11.94125 11.93585 11.93005 11.92628 11.92670 11.93068 11.93760 11.94682
## [265] 11.95773 11.96971 11.98212 11.99434 12.00576 12.01575 12.02367 12.02892
## [273] 12.03086 12.03347 12.04061 12.05153 12.06544 12.08158 12.09916 12.11743
## [281] 12.13561 12.15292 12.16859 12.18186 12.19195 12.19808 12.19949 12.19890
## [289] 12.19937 12.20063 12.20245 12.20454 12.20667 12.20856 12.20997 12.21063
## [297] 12.21028 12.20867 12.20554 12.20063 12.19369 12.18377 12.17041 12.15405
## [305] 12.13514 12.11412 12.09145 12.06756 12.04291 12.01793 11.99308 11.96881
## [313] 11.94555 11.92375 11.90387 11.88634 11.87162 11.85593 11.83574 11.81181
## [321] 11.78493 11.75586 11.72538 11.69427 11.66330 11.63324 11.60486 11.57895
## [329] 11.55627 11.53761 11.52372 11.51112 11.49612 11.47924 11.46100 11.44191
## [337] 11.42250 11.40328 11.38477 11.36747 11.35192 11.33862 11.32810 11.32086
## [345] 11.31744 11.31644 11.31615 11.31661 11.31785 11.31990 11.32279 11.32656
## [353] 11.33123 11.33684 11.34342 11.35100 11.35961 11.36928 11.38005 11.39330
## [361] 11.41015 11.43025 11.45324 11.47878 11.50652 11.53609 11.56715 11.59935
## [369] 11.63233 11.66575 11.69924 11.73246 11.76506 11.79668 11.82697 11.85558
## [377] 11.88215 11.90633 11.93160 11.96117 11.99433 12.03035 12.06851 12.10809
## [385] 12.14838 12.18865 12.22818 12.26626 12.30216 12.33517 12.36456 12.38961
## [393] 12.41346 12.43945 12.46716 12.49616 12.52605 12.55638 12.58675 12.61673
## [401] 12.64591 12.67385 12.70015 12.72438 12.74612 12.76494 12.78273 12.80146
## [409] 12.82086 12.84066 12.86060 12.88039 12.89977 12.91847 12.93622 12.95275
## [417] 12.96779 12.98106 12.99230 13.00125 13.00794 13.01272 13.01575 13.01717
## [425] 13.01715 13.01585 13.01340 13.00998 13.00573 13.00081 12.99538 12.98958
## [433] 12.98359 12.97754 12.97161 12.96593 12.95912 12.94988 12.93852 12.92534
## [441] 12.91067 12.89479 12.87802 12.86068 12.84306 12.82547 12.80823 12.79164
## [449] 12.77600 12.76163 12.74578 12.72593 12.70274 12.67686 12.64894 12.61963
## [457] 12.58960 12.55949 12.52995 12.50165 12.47523 12.45134 12.43065 12.41380
## [465] 12.39757 12.37866 12.35760 12.33491 12.31113 12.28678 12.26240 12.23851
## [473] 12.21565 12.19434 12.17511 12.15850 12.14504 12.13524 12.12751 12.11993
## [481] 12.11257 12.10553 12.09888 12.09270 12.08708 12.08209 12.07781 12.07433
## [489] 12.07173 12.07009 12.06948 12.07000 12.07121 12.07267 12.07447 12.07666
## [497] 12.07932 12.08250 12.08630 12.09076 12.09596 12.10196 12.10885 12.11667
## [505] 12.12551 12.13543 12.14622 12.15763 12.16967 12.18235 12.19570 12.20971
## [513] 12.22441 12.23980 12.25590 12.27272 12.29027 12.30857 12.32762 12.34745
## [521] 12.36806 12.38946 12.41167 12.43470 12.45856 12.48333 12.50904 12.53567
## [529] 12.56320 12.59160 12.62085 12.65091 12.68177 12.71339 12.74576 12.77885
## [537] 12.81263 12.84707 12.88216
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.3, n = 539)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 11.88641 11.88814 11.88989 11.89167 11.89345 11.89523 11.89702 11.89879
## [9] 11.90054 11.90227 11.90397 11.90562 11.90723 11.90879 11.91028 11.91171
## [17] 11.91306 11.91432 11.91550 11.91658 11.91756 11.91842 11.91917 11.91979
## [25] 11.92027 11.92062 11.92082 11.92086 11.92074 11.92046 11.91999 11.91935
## [33] 11.91851 11.91747 11.91623 11.91478 11.91309 11.91115 11.90897 11.90658
## [41] 11.90399 11.90122 11.89829 11.89522 11.89203 11.88873 11.88535 11.88189
## [49] 11.87839 11.87485 11.87129 11.86775 11.86422 11.86074 11.85731 11.85396
## [57] 11.85070 11.84756 11.84455 11.84169 11.83900 11.83650 11.83420 11.83212
## [65] 11.82975 11.82659 11.82270 11.81816 11.81301 11.80734 11.80120 11.79465
## [73] 11.78777 11.78060 11.77323 11.76570 11.75809 11.75045 11.74286 11.73538
## [81] 11.72806 11.72098 11.71419 11.70777 11.70177 11.69625 11.69129 11.68695
## [89] 11.68329 11.68037 11.67825 11.67701 11.67569 11.67336 11.67012 11.66607
## [97] 11.66130 11.65592 11.65003 11.64372 11.63710 11.63025 11.62329 11.61631
## [105] 11.60941 11.60269 11.59624 11.59017 11.58457 11.57955 11.57520 11.57163
## [113] 11.56892 11.56718 11.56651 11.56701 11.56877 11.57190 11.57650 11.58265
## [121] 11.59114 11.60243 11.61617 11.63199 11.64956 11.66852 11.68851 11.70918
## [129] 11.73018 11.75116 11.77176 11.79164 11.81043 11.82778 11.84335 11.86035
## [137] 11.88179 11.90698 11.93523 11.96586 11.99818 12.03151 12.06516 12.09845
## [145] 12.13069 12.16119 12.18927 12.21425 12.23543 12.25664 12.28171 12.30998
## [153] 12.34082 12.37355 12.40753 12.44212 12.47665 12.51047 12.54293 12.57338
## [161] 12.60117 12.62564 12.64615 12.66511 12.68524 12.70635 12.72822 12.75065
## [169] 12.77344 12.79638 12.81926 12.84188 12.86404 12.88552 12.90613 12.92565
## [177] 12.94389 12.96063 12.97567 12.98881 12.99984 13.00856 13.01607 13.02358
## [185] 13.03099 13.03822 13.04518 13.05179 13.05795 13.06358 13.06859 13.07290
## [193] 13.07641 13.07904 13.08071 13.08132 13.08079 13.07903 13.07596 13.07148
## [201] 13.06551 13.05796 13.04874 13.03778 13.02497 13.00882 12.98830 12.96400
## [209] 12.93655 12.90653 12.87457 12.84127 12.80724 12.77308 12.73940 12.70681
## [217] 12.67592 12.64733 12.62165 12.59391 12.55947 12.51943 12.47489 12.42698
## [225] 12.37679 12.32544 12.27402 12.22365 12.17544 12.13048 12.08990 12.05479
## [233] 12.02627 11.99990 11.97090 11.93979 11.90710 11.87335 11.83909 11.80482
## [241] 11.77110 11.73843 11.70737 11.67842 11.65213 11.62902 11.60962 11.59274
## [249] 11.57678 11.56170 11.54743 11.53392 11.52113 11.50898 11.49744 11.48644
## [257] 11.47593 11.46586 11.45617 11.44680 11.43771 11.43137 11.42982 11.43233
## [265] 11.43815 11.44656 11.45681 11.46817 11.47991 11.49128 11.50156 11.51000
## [273] 11.51588 11.51845 11.51698 11.51624 11.52086 11.52995 11.54258 11.55786
## [281] 11.57487 11.59270 11.61045 11.62720 11.64204 11.65407 11.66238 11.66605
## [289] 11.66418 11.65836 11.65096 11.64218 11.63224 11.62135 11.60971 11.59754
## [297] 11.58506 11.57248 11.56000 11.54785 11.53623 11.52535 11.51543 11.50371
## [305] 11.48762 11.46768 11.44440 11.41829 11.38985 11.35960 11.32804 11.29568
## [313] 11.26304 11.23062 11.19894 11.16850 11.13981 11.11338 11.08972 11.06934
## [321] 11.05275 11.04046 11.02975 11.01777 11.00479 10.99114 10.97709 10.96295
## [329] 10.94903 10.93560 10.92298 10.91146 10.90134 10.89292 10.88649 10.88235
## [337] 10.88080 10.88153 10.88394 10.88785 10.89312 10.89957 10.90704 10.91537
## [345] 10.92439 10.93395 10.94388 10.95402 10.96420 10.97426 10.98587 11.00050
## [353] 11.01776 11.03724 11.05855 11.08127 11.10500 11.12934 11.15389 11.17824
## [361] 11.20199 11.22473 11.24607 11.26559 11.28549 11.30798 11.33275 11.35944
## [369] 11.38771 11.41724 11.44767 11.47868 11.50993 11.54107 11.57177 11.60169
## [377] 11.63050 11.65785 11.68340 11.70683 11.73016 11.75544 11.78232 11.81043
## [385] 11.83943 11.86896 11.89867 11.92821 11.95722 11.98536 12.01226 12.03757
## [393] 12.06095 12.08204 12.10300 12.12598 12.15057 12.17637 12.20298 12.23000
## [401] 12.25703 12.28366 12.30950 12.33413 12.35717 12.37820 12.39684 12.41266
## [409] 12.42747 12.44314 12.45943 12.47607 12.49282 12.50943 12.52564 12.54120
## [417] 12.55587 12.56938 12.58149 12.59194 12.60048 12.60687 12.61068 12.61188
## [425] 12.61080 12.60778 12.60314 12.59722 12.59033 12.58282 12.57500 12.56722
## [433] 12.55980 12.55306 12.54735 12.54298 12.53791 12.53013 12.52003 12.50800
## [441] 12.49443 12.47972 12.46425 12.44842 12.43262 12.41724 12.40268 12.38931
## [449] 12.37754 12.36775 12.35776 12.34537 12.33097 12.31493 12.29765 12.27950
## [457] 12.26088 12.24217 12.22375 12.20600 12.18932 12.17408 12.16067 12.14948
## [465] 12.13918 12.12828 12.11691 12.10518 12.09321 12.08115 12.06909 12.05718
## [473] 12.04553 12.03426 12.02350 12.01338 12.00401 11.99552 11.98743 11.97923
## [481] 11.97097 11.96273 11.95456 11.94652 11.93867 11.93108 11.92382 11.91693
## [489] 11.91048 11.90453 11.89916 11.89440 11.89007 11.88590 11.88192 11.87815
## [497] 11.87461 11.87131 11.86828 11.86552 11.86307 11.86092 11.85912 11.85767
## [505] 11.85658 11.85589 11.85554 11.85548 11.85572 11.85624 11.85705 11.85815
## [513] 11.85954 11.86121 11.86318 11.86542 11.86795 11.87077 11.87387 11.87725
## [521] 11.88091 11.88485 11.88908 11.89358 11.89837 11.90345 11.90884 11.91453
## [529] 11.92054 11.92684 11.93344 11.94034 11.94753 11.95500 11.96277 11.97081
## [537] 11.97913 11.98773 11.99660
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")